# def sayHello():
#         res="Hello World1"
#         return res

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.patches import Rectangle
from scipy.interpolate import griddata
import matplotlib.colors as colors
from mpl_toolkits.axes_grid1 import make_axes_locatable
import json
import io

def initplot():
        print("___________________ERTPlot__________________121111")
        print("___________________count__________________ "+ str(count))
        '''
            本程序画出高密度电阻率法的等值线图和排列图
            :param IPValues:
            :return: NONE
        '''
        Aspacing = 20
        Max_n_spacing = 10
        num_electrodes = 20
        array_type = "dipole-dipole"
        NS = np.sum(range(1, Max_n_spacing + 1)) + Max_n_spacing * (
                (num_electrodes - 3) - Max_n_spacing)  # 总排列数
        ABMN = np.zeros((NS, 4))  # ABMN电极排列
        c = 0
        # 计算排列电极位置 （这里假设只有一个ADC接收电极，当有多个ADC时，此部分再修改)
        if array_type == "dipole-dipole":
                for i in range(num_electrodes - 3):
                        for j in range(i + 2, num_electrodes - 1):
                                ABMN[c, 0] = i + 1  # A电极
                                ABMN[c, 1] = i + 2  # B电极
                                ABMN[c, 2] = j + 1  # M电极
                                ABMN[c, 3] = j + 2  # N电极
                                N = ABMN[c, 2] - ABMN[c, 1]
                                if N > Max_n_spacing:
                                        continue
                                else:
                                        c = c + 1
        elif array_type == "pole-dipole":
                for i in range(num_electrodes - 3):
                        for j in range(i + 2, num_electrodes - 1):
                                ABMN[c, 0] = 1  # A电极, 整个测量中不动
                                ABMN[c, 1] = i + 2  # B电极
                                ABMN[c, 2] = j + 1  # M电极
                                ABMN[c, 3] = j + 2  # N电极
                                N = ABMN[c, 2] - ABMN[c, 1]
                                if N > Max_n_spacing:

                                        continue
                                else:
                                        c = c + 1
        # IPValues = {0: {'TXA': 0.0, 'TXB': 20.0, 'RXM': 40.0, 'RXN': 60.0, 'X': 30.0, 'Z': 20.0, 'n': 1.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 376.99111843077515}, 1: {'TXA': 0.0, 'TXB': 20.0, 'RXM': 60.0, 'RXN': 80.0, 'X': 40.0, 'Z': 30.0, 'n': 2.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 1507.9644737231006}, 2: {'TXA': 0.0, 'TXB': 20.0, 'RXM': 80.0, 'RXN': 100.0, 'X': 50.0, 'Z': 40.0, 'n': 3.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 3769.9111843077517}, 3: {'TXA': 0.0, 'TXB': 20.0, 'RXM': 100.0, 'RXN': 120.0, 'X': 60.0, 'Z': 50.0, 'n': 4.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 7539.822368615503}, 4: {'TXA': 0.0, 'TXB': 20.0, 'RXM': 120.0, 'RXN': 140.0, 'X': 70.0, 'Z': 60.0, 'n': 5.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 13194.689145077129}, 5: {'TXA': 0.0, 'TXB': 20.0, 'RXM': 140.0, 'RXN': 160.0, 'X': 80.0, 'Z': 70.0, 'n': 6.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 21111.50263212341}, 6: {'TXA': 0.0, 'TXB': 20.0, 'RXM': 160.0, 'RXN': 180.0, 'X': 90.0, 'Z': 80.0, 'n': 7.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 31667.253948185116}, 7: {'TXA': 0.0, 'TXB': 20.0, 'RXM': 180.0, 'RXN': 200.0, 'X': 100.0, 'Z': 90.0, 'n': 8.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 45238.93421169302}, 8: {'TXA': 0.0, 'TXB': 20.0, 'RXM': 200.0, 'RXN': 220.0, 'X': 110.0, 'Z': 100.0, 'n': 9.0,'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 62203.534541077905}, 9: {'TXA': 0.0, 'TXB': 20.0, 'RXM': 220.0, 'RXN': 240.0, 'X': 120.0, 'Z': 110.0, 'n': 10.0, 'TXI': 2.0,'RXV': 2.0, 'IP': 0.0, 'Rho': 82938.04605477054}, 10: {'TXA': 20.0, 'TXB': 40.0, 'RXM': 60.0, 'RXN': 80.0, 'X': 50.0, 'Z': 20.0, 'n': 1.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 376.99111843077515}, 11: {'TXA': 20.0, 'TXB': 40.0, 'RXM': 80.0, 'RXN': 100.0, 'X': 60.0, 'Z': 30.0, 'n': 2.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho':
        #     1507.9644737231006}, 12: {'TXA': 20.0, 'TXB': 40.0, 'RXM': 100.0, 'RXN': 120.0, 'X': 70.0, 'Z': 40.0, 'n': 3.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 3769.9111843077517}, 13: {'TXA': 20.0, 'TXB': 40.0, 'RXM': 120.0, 'RXN': 140.0, 'X': 80.0, 'Z': 50.0, 'n': 4.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 7539.822368615503}, 14: {'TXA': 20.0, 'TXB': 40.0, 'RXM': 140.0, 'RXN': 160.0, 'X': 90.0, 'Z': 60.0, 'n': 5.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 13194.689145077129}, 15: {'TXA': 20.0, 'TXB': 40.0, 'RXM': 160.0, 'RXN': 180.0, 'X': 100.0, 'Z': 70.0, 'n': 6.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 21111.50263212341}, 16: {'TXA': 20.0, 'TXB': 40.0, 'RXM': 180.0, 'RXN': 200.0, 'X': 110.0, 'Z': 80.0, 'n': 7.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 31667.253948185116}, 17: {'TXA': 20.0, 'TXB': 40.0, 'RXM': 200.0, 'RXN': 220.0, 'X': 120.0, 'Z': 90.0, 'n': 8.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 45238.93421169302}, 18: {'TXA': 20.0, 'TXB': 40.0, 'RXM': 220.0, 'RXN': 240.0, 'X': 130.0, 'Z': 100.0, 'n': 9.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 62203.534541077905}, 19: {'TXA': 20.0, 'TXB': 40.0, 'RXM': 240.0, 'RXN': 260.0, 'X': 140.0, 'Z': 110.0, 'n': 10.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 82938.04605477054}, 20: {'TXA': 40.0, 'TXB': 60.0, 'RXM': 80.0, 'RXN': 100.0, 'X': 70.0, 'Z': 20.0, 'n': 1.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 376.99111843077515}, 21: {'TXA': 40.0, 'TXB': 60.0, 'RXM': 100.0, 'RXN': 120.0, 'X': 80.0, 'Z': 30.0, 'n': 2.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 1507.9644737231006}, 22: {'TXA': 40.0, 'TXB': 60.0, 'RXM': 120.0, 'RXN': 140.0, 'X': 90.0, 'Z': 40.0, 'n': 3.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 3769.9111843077517}, 23: {'TXA': 40.0, 'TXB': 60.0, 'RXM': 140.0, 'RXN': 160.0, 'X': 100.0, 'Z': 50.0, 'n': 4.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 7539.822368615503}, 24: {'TXA': 40.0, 'TXB': 60.0, 'RXM': 160.0, 'RXN': 180.0, 'X': 110.0, 'Z': 60.0, 'n': 5.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 13194.689145077129}, 25: {'TXA': 40.0, 'TXB': 60.0, 'RXM': 180.0, 'RXN': 200.0, 'X': 120.0, 'Z': 70.0, 'n': 6.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 21111.50263212341}, 26: {'TXA': 40.0, 'TXB': 60.0, 'RXM': 200.0, 'RXN': 220.0, 'X': 130.0, 'Z': 80.0, 'n': 7.0, 'TXI': 2.0, 'RXV': 2.0, 'IP': 0.0, 'Rho': 31667.253948185116}}

        ####这里画出两个图，一个是排列图，利用红色圆圈表示发射，用黑色圆表示接收
        #最后一个IP的值
        #获取相应的画图参数
        # NIP = len(IPValues)
        # print("-=-=-=-=-=-= NIP === "+str(NIP))
        print("1111111111111111111111111111"+str(type(IPValues)))
        lastIP = json.loads(IPValues)
        print("-=-=-=-=-=-= lastIP === "+str(lastIP))

        X = lastIP['X']
        Z = lastIP['Z']
        rho = lastIP['Rho']
        # #####计算图的尺寸
        Min_X = np.min(ABMN[:, 0] - 1) * Aspacing
        Max_X = np.max(ABMN[:, 3] - 1) * Aspacing
        Min_Z = np.min(ABMN[:, 2] - ABMN[:, 1]) * Aspacing / 2 + Aspacing / 2
        Max_Z = np.max(ABMN[:, 2] - ABMN[:, 1]) * Aspacing / 2 + Aspacing / 2
        #fig=plt.subplots(figsize=(15,7.5))
        #fig1 = plt.figure(num=1)  # 创建一个新的窗口，所有参数采用默认
        #fig,ax=plt.subplots(figsize=(15,7.5))
        #fig,ax=plt.subplot(111)
        # 画出所有排列的地下记录点
        NS = np.size(ABMN, 0)

        #####
        # plt.draw()
        #ax = fig1.subplots(1,1)
        # if count == 0:
        # fig = plt.figure(figsize=(16,6))
        # ax = fig.add_subplot(111)
        colormap = cm.get_cmap('jet')
        rho_range = np.logspace(1, 5, 40)
        plt.xlim([Min_X, Max_X])
        plt.ylim([0, Max_Z])
        ###画出所有的IPvalues
        rho_trans = (np.log10(rho) - np.log10(min(rho_range))) / 4
        cls = colormap(rho_trans)
        # 画为圆或者方块都可以，这里选择了方块，使得两个点之间的电阻率图像可以连接起来
        # Rho_C = plt.Circle((X, Z), radius=Aspacing / 3, fill=True, color=cls, linewidth=2.0, \
        #                   clip_on=False)   # 拟断面图画图点
        # plt.gca().add_artist(Rho_C)
        Rho_rect = Rectangle((X - Aspacing / 2, Z - Aspacing / 4), Aspacing, Aspacing / 2 * 0.95,
                             fill=True, color=cls, \
                             clip_on=False)  # 拟断面图画图点
        plt.gca().add_patch(Rho_rect)  # 将矩形画在坐标轴上
        plt.annotate(format(rho, '.0f'), xy=(X, Z), fontsize=8, ha='center', va='center')  # 将数值显示在矩形里

        #plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
        plt.gca().invert_yaxis()  # x轴纵向翻转
        plt.gca().set_xlabel('Array Location(m)', size=14)  # 设置x轴的标题
        plt.gca().set_ylabel('n-spacing', size=14)  # 设置y轴的标题
        plt.gca().xaxis.set_label_position('top')  # 设置x标题置顶
        plt.gca().xaxis.tick_top()  # 设置x轴置顶
        plt.gca().set_aspect('equal')  # 设置长宽比
        # print("+_+_+_+_+_+_++_+_+"+str(Min_Z)+" _+ "+str(Max_Z)+" _+ "+str(Aspacing)
        # plt.yticks(np.insert(np.arange(Min_Z, Max_Z + Aspacing / 2, Aspacing / 2), 0, 0))  # y轴刻度
        # plt.xticks(np.arange(Min_X, Max_X, Aspacing))  # x轴刻度
        # labels = [item.get_text() for item in ax.get_yticklabels()]
        # NLAB = len(labels)
        # labs = np.insert(np.arange(int(np.min(ABMN[:, 2] - ABMN[:, 1])), \
        #                            int(np.max(ABMN[:, 2] - ABMN[:, 1]) + 1), 1), 0,
        #                  0)  # 重新设置Y轴数值 arange(起点值，终点值，步长)
        # if NLAB == len(labs):
        #     ax.set_yticklabels(labs)
        # ax.spines['bottom'].set_visible(False)
        # divider = make_axes_locatable(ax)
        # cax = divider.append_axes('right', size="2%", pad=0.1)  # 设置色段轴
        # sm = plt.cm.ScalarMappable(norm=colors.LogNorm(), cmap=colormap)
        # sm.set_clim(vmin=10, vmax=10000)
        # cbar = plt.colorbar(sm, cax=cax)  # 显示色段轴
        # cbar.set_label('Apparent Resistivity(Ohm-m)', rotation=270, labelpad=20,
        #                fontsize='medium')  # 设置色段轴标题
        plt.draw()
        plt.pause(0.01)
        # cbar.remove()
        # plt.close()
        ##最终画出等值线图
        # plt.show()

        f = io.BytesIO()
        plt.savefig(f, format="png")
        print("___________________ERTPlot__________________22222")

        return f.getvalue()

